var $ = require("../../utils/Ajax.js");
function goodsList(that, type, ShopClassId){
  $.Ajax("api/goodsList", "GET", {
    od: type,
    cid: ShopClassId
  }, function (res) {
    console.log(res)
    if (res.s) {
      that.setData({
        ShopData: res.d.goods.data,
        ShopClass: res.d.goodsClass
      })
    } else {
      wx.showToast({
        title: res.m,
        icon: 'none',
        duration: 3000,
        mask: true
      })
    }
  })
}
Page({
  data: {
    // 图片路径配置
    PictureUrl: getApp().PictureUrl,

    // 点击返回顶部
    ScrollBackTop: getApp().ScrollBackTop,

    // 距离底部距离,开始请求数据
    ScrollPostlower: getApp().ScrollPostlower,

    // 滑动购物车按钮时,禁止页面滚动
    Scroll: true,

    // 商城数据
    ShopData: null,

    // 销量 - 上架时间
    Condition:[
      { type: "1", name: "销量"},
      { type: "2", name: "上架时间"}
    ],

    // 销量 - 上架时间下标
    Index: 0,
    Type: 1,
    ShopClassId: 0,

    // 商城数据分类
    ShopClass: null,

    // 购物车移动位置
    top: null,
    left: null,

    // 购物车商品数量
    Quantity: 0,

    // 选择分类控制
    showShopPopup: false,
    animationData: {}

  },

  onShow: function () {
    var that = this;
    var Token = wx.getStorageSync('Token');
    var UserInfor = wx.getStorageSync('UserInfor');
    if (Token == '' || Token == null) {
      wx.removeStorageSync('Token');
      wx.removeStorageSync('UserInfor');
      wx.switchTab({
        url: '../My/My',
      })
    }else{
      $.Ajax("api/cart", "GET", {}, function (res) {
        if (res.s) {
          that.setData({
            Quantity: res.d.cartNum
          })
        } else {
          wx.showToast({
            title: res.m,
            icon: 'none',
            duration: 2000
          })
        }
      })
    }
  },

  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "商城"
    }) 
    goodsList(this, this.data.Type, this.data.ShopClassId);
  },

  // 点击销量 / 上架时间
  Condition: function(e){
    var Type = e.currentTarget.dataset.type;
    var Index = e.currentTarget.dataset.index_;
    this.setData({
      Index: Index,
      Type: Type
    })
    goodsList(this, this.data.Type, this.data.ShopClassId);
  },

  // 点击选择商品分类
  ClassType: function(){
    var animation = wx.createAnimation({
      duration: 400,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(-400).step()
    this.setData({
      animationData: animation.export(),
      showShopPopup: true
    })
    setTimeout(() => {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }, 200)
  },

  // 选择商品分类
  ShopClassId: function(e){
    var Id = e.currentTarget.dataset.id;
    this.setData({
      ShopClassId: Id
    })
    goodsList(this, this.data.Type, this.data.ShopClassId);

    this.ClassTypeBgClose();
  },

  // 点击关闭商品分类
  ClassTypeBgClose: function(){
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(-400).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showShopPopup: false
      })
    }.bind(this), 200)
  },

  // 点击打开购物车
  Cart: function(){
    var Token = wx.getStorageSync('Token');
    var UserInfor = wx.getStorageSync('UserInfor');
    if (Token == '' || Token == null) {
      wx.removeStorageSync('Token');
      wx.removeStorageSync('UserInfor');
      wx.showToast({
        title: "请先登录",
        icon: 'none',
        duration: 1500
      })
      setTimeout(function () {
        wx.switchTab({
          url: '../My/My',
        })
      }, 1500)
    } else {
      wx.navigateTo({
        url: '../Cart/Cart',
      })
    }
  },

  // 手指拖动购物车按钮
  setTouchMove: function (e) {
    //此处clientY与clientX为拖动悬浮窗超过设定的大小会返回默认显示位置
    if (e.touches[0].clientX > 30 && e.touches[0].clientY > 80 && e.touches[0].clientX < 370 && e.touches[0].clientY < 600) {
      this.setData({
        Scroll: false,
        left: e.touches[0].clientX - 20,
        top: e.touches[0].clientY - 20
      })
    }
  },

  // 手指离开购物车按钮
  setTouchEnd: function(){
    this.setData({
      Scroll: true
    })
  },

  // 点击进入商品详情
  ShopDetails: function(e){
    var Id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../ShopDetails/ShopDetails?Id=' + Id + '&Type=0',
    })
  }
})